package com.kvest.fragment_test;

import android.app.Activity;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.widget.TextView;
import com.kvest.fragment_test.fragment.FragmentOne;
import com.kvest.fragment_test.fragment.FragmentTwo;

public class FragmentTestActivity extends FragmentActivity
        implements FragmentOne.FragmentOneListener {
    protected static final String LOG_TAG = "FragmenTest";

    private FragmentTwo fragment;
    private FragmentOne nextFragment;

    /*For test*/
    1) Уроки
    2) viewnIsst
    4) Tabbar + ViewPager
    /**/

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Log.d(LOG_TAG, "FragmentTestActivity onCreate");

        if (savedInstanceState == null) {
            fragment = new FragmentTwo();
            fragment.setArguments(getIntent().getExtras());
            FragmentTransaction transacion = getSupportFragmentManager().beginTransaction();
            try {
                transacion.add(R.id.fragment_three_container, fragment);
            } finally {
                transacion.commit();
            }

            nextFragment = new FragmentOne();
            nextFragment.setArguments(getIntent().getExtras());
        }

        Fragment f = getSupportFragmentManager().findFragmentById(R.id.frg_one);
        if (f != null) {
            ((TextView)f.getView().findViewById(R.id.log_tv)).setText("Hello from activity");
        }
    }

    protected void onStart() {
        super.onStart();
        Log.d(LOG_TAG, "FragmentTestActivity onStart");
    }

    protected void onResume() {
        super.onResume();
        Log.d(LOG_TAG, "FragmentTestActivity onResume");
    }

    protected void onPause() {
        super.onPause();
        Log.d(LOG_TAG, "FragmentTestActivity onPause");
    }

    protected void onStop() {
        super.onStop();
        Log.d(LOG_TAG, "FragmentTestActivity onStop");
    }

    protected void onDestroy() {
        super.onDestroy();
        Log.d(LOG_TAG, "FragmentTestActivity onDestroy");
    }

    public void onDeleteClick() {
        FragmentTransaction t = getSupportFragmentManager().beginTransaction();
        try {
            t.remove(getSupportFragmentManager().findFragmentById(R.id.fragment_three_container));
            t.addToBackStack(null);
        } finally {
            t.commit();
        }

    }

    public void onReplaceClick() {
        FragmentTransaction t = getSupportFragmentManager().beginTransaction();
        try {
            t.replace(R.id.fragment_three_container, nextFragment);
            t.addToBackStack(null);
        } finally {
            t.commit();
        }
    }
}
